package com.wm.ucenterService.controller;


import com.wm.commonutils.JwtUtils;
import com.wm.commonutils.R;
import com.wm.commonutils.vo.MemberOrderVo;
import com.wm.ucenterService.entity.UcenterMember;
import com.wm.ucenterService.entity.vo.RegisterVo;
import com.wm.ucenterService.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author sshdg
 * @since 2020-10-09
 */
@RestController
@RequestMapping("/eduCenter/member")
@Api(tags = "会员管理 ")
//@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    @PostMapping("/login")
    @ApiOperation("会员登录")
    public R login(@RequestBody UcenterMember ucenterMember){
        //memberForm 对象封装手机号和密码

        //返回token 值,使用jwt生成
        String token = memberService.login(ucenterMember);

        return R.ok().data("token",token);
    }

    /**
     * 用户注册
     *
     */
    @PostMapping("/register")
    @ApiOperation("会员注册")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 获取会员信息
     */
    @ApiOperation("获取会员信息")
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据Request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //根据数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("member",member);
    }

    @ApiOperation("根据会员id获取用户信息")
    @GetMapping("/getMemberInfoOrder/{id}")
    public MemberOrderVo getMemberInfoOrderById(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        MemberOrderVo memberOrderVo = new MemberOrderVo();
        BeanUtils.copyProperties(member,memberOrderVo);
        return memberOrderVo;
    }
    @GetMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable String day){
        Integer count =  memberService.countRegisterDay(day);
        return count;
    }
}

